import Vue from 'vue'

const actions = {
  // 下载文件
  downFile ({ commit }, payload) {
    return new Promise((resolve, reject) => {
      let th = payload.th || 1
      let closeLoading = th === 4
      let data = {th: th}
      if (payload.url) {
        data['url'] = payload.url
      } else {
        data['fileUuid'] = payload.fileUuid
      }
      /* th
      * 1: 原图
      * 2: 中图
      * 3: 小图 180*180
      * 4: 小图 120*120 */
      Vue.prototype.$axios({
        method: 'post',
        url: `${Vue.prototype.$site.homeServer}/common/fileDownloadUrl.htm`,
        closeSaveRequest: true,
        closeLoading: closeLoading,
        data: Vue.prototype.$qs.stringify(data)
      }).then(res => {
        // 成功回调
        if (res && res.data && res.data.errorMsg) {
          Vue.prototype.$message.error(res.data.errorMsg)
          resolve('')
        } else {
          resolve(res.data)
        }
      }, err => {
        reject(err)
      })
    })
  }
}

export default {
  actions
}
